package com.rjwh.dingdong.client.activity.more;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.rjwh.dingdong.client.R;
import com.rjwh.dingdong.client.activity.BaseActivity;
import com.rjwh.dingdong.client.adapter.MoreBabyInfoListLvAdapter;
import com.rjwh.dingdong.client.application.MyApplication;
import com.rjwh.dingdong.client.bean.localbean.SysLoginBabylistBean;
import com.rjwh.dingdong.client.constant.LocalConstant;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author Kay
 * @ClassName MoreBabyInfoListActivity
 * @Description
 * @date 2016-7-20
 * @history 2016-7-20 author: description:
 */
public class MoreBabyInfoListActivity extends BaseActivity implements AdapterView.OnItemClickListener {


    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.app_bar_layout)
    AppBarLayout appBarLayout;
    @BindView(R.id.more_baby_info_list_lv)
    ListView moreBabyInfoListLv;
    private MoreBabyInfoListLvAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.more_baby_info_list_layout);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        initToolbarHelper();
        List<SysLoginBabylistBean> babyInfoList = MyApplication.db.findAll(SysLoginBabylistBean.class);
        adapter = new MoreBabyInfoListLvAdapter(this);
        moreBabyInfoListLv.setAdapter(adapter);
        if (babyInfoList != null) {
            adapter.appendToList(babyInfoList);
        }
        moreBabyInfoListLv.setOnItemClickListener(this);
    }

    private void initToolbarHelper() {
        this.setSupportActionBar(this.toolbar);
        getSupportActionBar().setTitle("宝宝帐号");
        getSupportActionBar().setHomeButtonEnabled(true); //设置返回键可用
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        SysLoginBabylistBean item = (SysLoginBabylistBean) parent.getAdapter().getItem(position);
        if (!item.getChief().equals("1")) {
            item.setChief("1");
            MyApplication.db.update(item);
            MyApplication.spUtil.setStrPreference(this, LocalConstant.SP_BABYID, item.getBabyid());
            MyApplication.spUtil.setStrPreference(this, LocalConstant.SP_BABYNAME, item.getBabyname());
            MyApplication.spUtil.setStrPreference(this, LocalConstant.SP_BABYSEX, item.getSex());
            MyApplication.spUtil.setStrPreference(this, LocalConstant.SP_BABYBIRTHDAY, item.getBirthday());
            MyApplication.spUtil.setStrPreference(this, LocalConstant.SP_USERCHENGWEI, item.getTitle());
            MyApplication.spUtil.setStrPreference(this, LocalConstant.SP_BABYAVATAR, item.getBabyavatar());
        }
        adapter.notifyDataSetChanged();
        Intent intent = new Intent(this, MoreBabyInfoActivity.class);
        startActivity(intent);
    }
}
